package com.zimin.cloud.wiki.biz.impl;

import com.zimin.cloud.mine.pojo.wiki.business.DocBO;
import com.zimin.cloud.mine.pojo.wiki.view.DocVO;
import com.zimin.cloud.wiki.biz.IContentBiz;
import com.zimin.cloud.wiki.biz.IDocBiz;
import com.zimin.cloud.wiki.pojo.entity.Content;
import com.zimin.cloud.wiki.service.impl.DocServiceImpl;
import com.zimin.common.annotation.Biz;
import com.zimin.common.enums.Number;
import com.zimin.common.utils.ObjectUtils;
import com.zimin.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 文档 业务对象
 * </p>
 *
 * @author zimin
 * @since 2021-10-27 21:02
 */
@Biz
public class DocBizImpl extends DocServiceImpl implements IDocBiz {
    @Autowired
    private IContentBiz contentBiz;

    @Override
    public List<DocVO> allTree(DocBO docBO) {
        List<DocVO> allDocVO = list(docBO);
        if (ObjectUtils.isEmpty(allDocVO)) {
            return List.of();
        }
        return tree(allDocVO, Number.ZERO.getStrNum());
    }

    @Override
    public boolean save(DocBO docBO) {
        boolean flag = super.save(docBO);
        if (flag) {
            if (StringUtils.isNotEmpty(docBO.getContent())) {
                String id = docBO.getId();
                Content content = new Content();
                content.setId(id);
                content.setContent(docBO.getContent());
                return contentBiz.save(content);
            }
        }
        return flag;
    }

    @Override
    public boolean updateById(DocBO docBO) {
        boolean flag = super.updateById(docBO);
        if (flag) {
            String id = docBO.getId();
            Content content = contentBiz.getById(id);
            if (ObjectUtils.isEmpty(content)) {
                content = new Content();
                content.setId(id);
                content.setContent(docBO.getContent());
                contentBiz.save(content);
            } else {
                content.setContent(docBO.getContent());
                return contentBiz.updateById(content);
            }
        }
        return flag;
    }


    /**
     * <p>获取分类树</p>
     *
     * @param docVOList 原List
     * @param parent    上级父ID
     * @return java.util.List<com.zimin.cloud.mine.pojo.wiki.view.DocVO>
     * @since 2021-11-24 16:55:59
     */
    private List<DocVO> tree(List<DocVO> docVOList, String parent) {
        Map<String, List<DocVO>> childrenMap = docVOList.stream()
                .collect(Collectors.groupingBy(DocVO::getParent));
        List<DocVO> children = childrenMap.get(parent);
        children.forEach(docVO -> {
            String parentNodeId = docVO.getId();
            List<DocVO> docVOList1 = childrenMap.get(parentNodeId);
            if (ObjectUtils.isNotEmpty(docVOList1)) {
                List<DocVO> tree = tree(docVOList, parentNodeId);
                docVO.setChildren(tree);
            }
        });
        return children;
    }
}
